﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinCourseControls.DAL;
using WinCourseControls.Models;

namespace WinCourseControls
{
    public partial class FrmMenuStrip : Form
    {
        public FrmMenuStrip()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 菜单项事件
        /// 手动添加项，响应：注册N个事件处理程序 -----工作量相当大   不可取
        /// 绝大多数：打开相应页面或少部分是执行一操作或任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miAddGrade_Click(object sender, EventArgs e)
        {
            //显示新增年级信息页面
            MForms.FrmAddGrade fAddGrade = new MForms.FrmAddGrade();
            fAddGrade.Show();
        }
        
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMenuStrip_Load(object sender, EventArgs e)
        {
            ////代码添加菜单项
            //ToolStripMenuItem miStudent = new ToolStripMenuItem();
            //miStudent.Name = "miStudent";
            //miStudent.Text = "学生管理";
            //menuStudent.Items.Add(miStudent);//添加一级菜单项

            ////它下面还有子菜单
            //ToolStripMenuItem miAddStudent = new ToolStripMenuItem();
            //miAddStudent.Name = "miAddStudent";
            //miAddStudent.Text = "新增学生";
            //miStudent.DropDownItems.Add(miAddStudent);

            menuStudent.Items.Clear();//清空菜单项 
            MenuDAL menuDAL = new MenuDAL(); // 数据库访问实例
            List<MenuInfo> menuList = menuDAL.GetModelList("", "", 0); // 获取菜单相数据
            CreateMenuItem(menuList, null, 0); // 递归加载菜单相
        } 

        /// <summary>
        /// 添加菜单项（递归）
        /// </summary>
        /// <param name="allList"></param>
        /// <param name="pNode"></param>
        /// <param name="parentId"></param>
        private void CreateMenuItem(List<MenuInfo> allList, ToolStripMenuItem pMenu, int parentId)
        {
            //获取指定的子菜单项数据
            List<MenuInfo> subMenus = allList.Where(m => m.ParentId == parentId).ToList(); // 获取当前层级菜单相
            if (subMenus.Count > 0) // 判断
            {
                foreach (MenuInfo child in subMenus) // 循环菜单相
                {
                    ToolStripMenuItem tmi = new ToolStripMenuItem(); // 实例化菜单相
                    tmi.Name = child.MenuId.ToString(); // 菜单控件名称
                    tmi.Text = child.MenuName; // 菜单显示名称
                    if(!string.IsNullOrEmpty(child.MenuKey))//设置了快捷键 - 判断是否有快捷键
                    {
                        string key = child.MenuKey.ToString().Trim(); // 获取快捷键
                        //alt快捷键
                        if(key.Length>1&&key.Split('+')[0].ToLower()=="alt" && child.ParentId==0)
                        { 
                            tmi.Text += $"(&{key.Split('+')[1]})"; // alt快捷键的显示文本需要单独处理
                        }
                        tmi.ShortcutKeys = SetShortKeys(key); // 设置关联的快捷键
                    }
                    if(allList.Where(m=>m.ParentId==child.MenuId).ToList().Count==0) // 判断菜单没有子项菜单
                    {
                        tmi.Click += Tmi_Click; // 给没有子项菜单的菜单添加单机事件
                    }

                    //该菜单项是一级菜单还是指定菜单项的子菜单
                    if (pMenu == null)
                    {
                        menuStudent.Items.Add(tmi); // 添加菜单项
                    }
                    else
                        pMenu.DropDownItems.Add(tmi); // 添加菜单项
                    //以tmi为父菜单项，去添加它的子菜单项
                    CreateMenuItem(allList, tmi, child.MenuId);
                }
            }
        }

        /// <summary>
        /// 菜单项的Click事件处理程序
        /// 没有子项菜单的菜单单机事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Tmi_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem mi = sender as ToolStripMenuItem; // 菜单项实例
            // 显示项文本
            MessageBox.Show(mi.Text);//点击了菜单项
        }

        ///<summary>
        /// 获取快捷键  Ctrl+C  Alt+F   Shift+P .....
        /// </summary>
        /// <param name="mkey"></param>
        /// <returns></returns>
        private Keys SetShortKeys(string mkey)
        {
            Keys reKey = default(Keys); // 声明快捷键
            string[] arrKeys = mkey.Split('+'); // 快捷建分割
            if (arrKeys.Length == 2) // 两个快捷键
            {
                Keys k;
                Enum.TryParse<Keys>(arrKeys[1], out k); // 讲字符串转成快捷建
                if (arrKeys[0].ToLower() == "ctrl")
                {
                    reKey = Keys.Control | k; // 代表 Ctrl + k建  设置快捷建相加语法
                }
                else if (arrKeys[0].ToLower() == "alt")
                {
                    reKey = Keys.Alt | k;
                }
                else if (arrKeys[0].ToLower() == "shift")
                {
                    reKey = Keys.Shift | k;
                }
            }
            else if (arrKeys.Length == 3) // 3个快捷键
            {
                Keys k;
                Enum.TryParse<Keys>(arrKeys[2], out k);
                string start = arrKeys[0].ToLower() + "+" + arrKeys[1].ToLower();
                switch (start)
                {
                    case "shift+alt":
                        reKey = Keys.Shift | Keys.Alt | k;
                        break;
                    case "ctrl+alt":
                        reKey = Keys.Control | Keys.Alt | k;
                        break;
                    case "ctrl+shift":
                        reKey = Keys.Control | Keys.Shift | k;
                        break;
                }
            }
            else if (arrKeys.Length == 4) // 4个快捷键
            {
                Keys k;
                Enum.TryParse<Keys>(arrKeys[3], out k);
                reKey = Keys.Control | Keys.Shift | Keys.Alt | k;
            }
            return reKey;

        }
    }
}
